import base from './baseUrl.js'
import toast from '@/util/toast.js'
export default class Request {
    http(param) {
        // 请求参数
        let url = param.url;
        let method = param.method;
        let header = param.header || {};
        let data = Object.assign(param.data || {});
        let hideLoading = param.hideLoading || false;
        // let whetherData = param.whetherData;
        var urls = ''
        if (param.type == 'mail') {
            // 发送短信 智能问答 代理人二维码
            urls = 'https://zw.zwfwpt.cn/diving-api/enjoy/pass'
        }
        else {
            urls = base.baseUrl
        } 
        //拼接完整请求地址
        let requestUrl = urls + url;

        //请求方式:GET或POST(POST需配置
        // header: {'content-type' : "application/x-www-form-urlencoded"},)
        if (method) {
            method = method.toUpperCase(); //小写改为大写
            if (method == "POST") {
                header = Object.assign({
                    // 'content-type': "application/x-www-form-urlencoded;charset=UTF-8"
                    'content-type': "application/json;charset=UTF-8"
                }, param.header, {
                    // tokenid: uni.getStorageSync("TOKEN")
                })
            } else {
                header = Object.assign({
                    'content-type': "application/json;charset=UTF-8"
                }, param.header, {
                    tokenid: uni.getStorageSync("TOKEN")
                    // Authorization: uni.getStorageSync("token")
                })
            }
        }
        //加载圈
        if (!hideLoading) {
            uni.showLoading({
                title: '加载中...'
            })
        }
        // 返回promise
        return new Promise((resolve, reject) => {
            // 请求
            uni.request({
                url: requestUrl,
                data: data,
                method: method,
                header: header,
                success: (res) => {
                    // console.log('拦截器', res);
                    //隐藏加载
                    if (!hideLoading) {
                        uni.hideLoading();
                    }
                    // 判断 请求是否成功 
                    if (res.statusCode == 200) {
                        if (res.data.code == 200) {
                            resolve(res.data)
                        } else {
                            // resolve(res.data)
                            reject(res.data)
                            // toast('服务器正忙~')
                            return false;
                        }
                    } else {
                        reject(res.data)
                        toast('服务器正忙~')
                        return false;
                    }
                },
                //请求失败
                fail: (err) => {
                    console.log('请求失败', err);
                    //隐藏加载
                    if (!hideLoading) {
                        uni.hideLoading();
                    }
                    toast('服务器正忙~')
                    return false;
                },
                //请求完成
                complete() {
                    return;
                }
            })
        })
    }
}
